package com.ruoyi.web.controller.web;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.GuiSecurityCode;
import com.ruoyi.system.service.IGuiSecurityCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author: jinpeng
 * @date: 2024/1/2 14:14
 * @description:
 */
@Anonymous
@RestController
@RequestMapping("/web")
public class WebIndexController extends BaseController {
    @Autowired
    private IGuiSecurityCodeService guiSecurityCodeService;

    @GetMapping("/query")
    public AjaxResult query(GuiSecurityCode queryVo) {
        if (queryVo.getSecurityNumber() == null) {
            AjaxResult.error("验证码不能为空");
        }
        if (queryVo.getProjectId() == null) {
            return AjaxResult.error("产品不能为空");
        }
        List<GuiSecurityCode> guiSecurityCodes = guiSecurityCodeService.selectGuiSecurityCodeList(queryVo);
        if (guiSecurityCodes.isEmpty()) {
            return AjaxResult.error(6001, "验证码错误");
        }
        if (guiSecurityCodes.size() > 1) {
            return AjaxResult.error(6002, "验证码错误");
        }
        GuiSecurityCode guiSecurityCode = guiSecurityCodes.get(0);
        guiSecurityCode.setNumber(guiSecurityCode.getNumber() + 1);
        guiSecurityCodeService.updateGuiSecurityCode(guiSecurityCode);
        return AjaxResult.success(guiSecurityCode);
    }

}
